﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sinacor.Infra.Tools;
using System.Collections.Generic;

namespace EndPointRegistrationWeb
{

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack && hdnDeleteAction.Value == "S")
            {
                DeleteItems();
                hdnDeleteAction.Value = "";
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btSave.Click += new EventHandler(btSave_Click);

            preencherDrops(); //inc - esertorio - 200810101255
        }

        void btSave_Click(object sender, EventArgs e)
        {
            bool IsUpdate = (!String.IsNullOrEmpty(hdnCodigoEndPoint.Value));

            try
            {
                if (ValidaCampos())
                {
                    EndPoint endpoint = new EndPoint();
                    endpoint.Contrato = txtContrato.Text.Trim();
                    //endpoint.Modulo = txtModulo.Text.Trim();
                    endpoint.Modulo = ddlModulo.SelectedValue;
                    
                    //endpoint.Protocolo = txtProtocolo.Text.Trim();
                    if (cmbProtocolo.Value != null)
                        endpoint.Protocolo = cmbProtocolo.Value.Trim();
                    
                    //endpoint.SubModulo = txtSubModulo.Text.Trim();
                    endpoint.SubModulo = ddlSubModulo.SelectedValue;

                    if (IsUpdate)
                    {
                        endpoint.CodigoEndPoint = Convert.ToInt32(hdnCodigoEndPoint.Value);
                        if (new DataAccess().UpdateEndPoint(endpoint) > 0)
                        {
                            lblMessage.Text = "<b><font color=#009900>Item \"" + endpoint.Contrato + "\" salvo com sucesso!</font></b>";
                            txtContrato.Text = "";
                            //txtModulo.Text = "";
                            ddlModulo.SelectedValue = "";
                            //txtSubModulo.Text = "";
                            ddlSubModulo.SelectedValue = "";
                            //txtProtocolo.Text = "";
                            cmbProtocolo.SelectedIndex = 0;
                        }
                        else
                            lblMessage.Text = "<b><font color=#990000>Nenhuma linha alterada</font></b>";

                        hdnCodigoEndPoint.Value = "";
                    }
                    else
                    {
                        if (new DataAccess().SaveEndPoint(endpoint) > 0)
                        {
                            lblMessage.Text = "<b><font color=#009900>Item \"" + endpoint.Contrato + "\" salvo com sucesso!</font></b>";
                            txtContrato.Text = "";
                            //txtModulo.Text = "";
                            ddlModulo.SelectedValue = "";
                            //txtSubModulo.Text = "";
                            ddlSubModulo.SelectedValue = "";
                            //txtProtocolo.Text = "";
                            cmbProtocolo.SelectedIndex = 0;
                        }
                        else
                            lblMessage.Text = "<b><font color=#990000>Nenhuma linha inserida</font></b>";
                    }
                }
                else
                    lblMessage.Text = "<b><font color=#990000>Informe pelo menos o contrato e o protocolo para efetuar uma inclusão</font></b>";
            }
            catch (Exception ex)
            {
                lblMessage.Text = "<b><font color=#990000>" + ex.Message + "</font></b>";
            }
        }

        void DeleteItems()
        {
            try
            {
                if (!String.IsNullOrEmpty(hdnSelected.Value))
                {
                    string[] comma = hdnSelected.Value.Split(',');

                    List<EndPoint> endpoints = new List<EndPoint>();

                    foreach (string s in comma)
                    {
                        endpoints.Add(new EndPoint(Convert.ToInt32(s)));
                    }

                    if (endpoints.Count > 0)
                    {
                        int linhasAfetadas = new DataAccess().DeleteEndPoint(endpoints);
                        if (linhasAfetadas > 0)
                        {
                            if (linhasAfetadas == 1)
                                lblMessage.Text = "<b><font color=#009900>" + linhasAfetadas.ToString() + " item deletado com sucesso!</font></b>";
                            else
                                lblMessage.Text = "<b><font color=#009900>" + linhasAfetadas.ToString() + " itens deletados com sucesso!</font></b>";
                        }
                        else
                            lblMessage.Text = "<b><font color=#990000>Nenhuma linha deletada</font></b>";
                    }
                    else
                        lblMessage.Text = "<b><font color=#990000>Nenhum item selecionado para exclusão!</font></b>";
                }
                else
                    lblMessage.Text = "<b><font color=#990000>Nenhum item selecionado para exclusão!</font></b>";
            }
            catch (Exception ex)
            {
                lblMessage.Text = "<b><font color=#990000>" + ex.Message + "</font></b>";
            }
        }

        bool ValidaCampos()
        {
            if (String.IsNullOrEmpty(txtContrato.Text.Trim()))
                return false;
            //else if (String.IsNullOrEmpty(txtProtocolo.Text.Trim()))
            else if (cmbProtocolo.Value == null || String.IsNullOrEmpty(cmbProtocolo.Value.Trim()))
                return false;
            else
                return true;
        }

        //inc - esertorio - 200810101158
        protected void ddlModulo_SelectedIndexChanged(object sender, EventArgs e)
        {
            preencherDropSubModulos(this.ddlModulo.SelectedValue);
        }

        //inc - esertorio - 200810101255
        protected void preencherDrops()
        {
            this.preencherDropModulos();
            this.preencherDropSubModulos();
        }

        //inc - esertorio - 200810101158
        private void preencherDropModulos()
        {
            this.ddlModulo.DataTextField = "Descricao";
            this.ddlModulo.DataValueField = "Id";

            Sinacor.Infra.Tools.ModuloDataAcess daModulo = new Sinacor.Infra.Tools.ModuloDataAcess();
            this.ddlModulo.DataSource = daModulo.consultar();
            this.ddlModulo.DataBind();
            this.ddlModulo.Items.Insert(0, new ListItem(string.Empty, string.Empty));
        }

        //inc - esertorio - 200810101158
        private void preencherDropSubModulos()
        {
            this.ddlSubModulo.DataTextField = "Descricao";
            this.ddlSubModulo.DataValueField = "Id";

            Sinacor.Infra.Tools.SubModuloDataAcess daSubModulo = new Sinacor.Infra.Tools.SubModuloDataAcess();
            this.ddlSubModulo.DataSource = daSubModulo.consultar();
            this.ddlSubModulo.DataBind();
            this.ddlSubModulo.Items.Insert(0, new ListItem(string.Empty, string.Empty));
        }

        //inc - esertorio - 200810101158
        private void preencherDropSubModulos(string IdSubModulo)
        {
            this.ddlSubModulo.DataTextField = "Descricao";
            this.ddlSubModulo.DataValueField = "Id";

            Sinacor.Infra.Tools.SubModuloDataAcess daSubModulo = new Sinacor.Infra.Tools.SubModuloDataAcess();
            Sinacor.Infra.Tools.SubModulo entSubModulo = new Sinacor.Infra.Tools.SubModulo();
            entSubModulo.Modulo = IdSubModulo;
            this.ddlSubModulo.DataSource = daSubModulo.consultar(entSubModulo);
            this.ddlSubModulo.DataBind();
            this.ddlSubModulo.Items.Insert(0, new ListItem(string.Empty, string.Empty));
        }

    }
}